﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using Comdiv.QWeb.Files;
using Comdiv.QWeb.Logging;
using NUnit.Framework;

namespace Comdiv.QWeb.Test.Logging
{
	[TestFixture]
	public class default_error_logger_test
	{
		private string dir;
		private QWebContext ctx;
		private FileNameResolver res;
		private DefaultApplicationErrorLogger log;

		[SetUp]
		public void setup()
		{
			this.dir = Path.Combine(Path.GetTempPath(), "default_error_logger_test");
			if (Directory.Exists(dir)) Directory.Delete(dir, true);
			Directory.CreateDirectory(dir);
			this.ctx = new QWebContext { RootDirectory = dir, ApplicationName = "/app" };
			this.res = new FileNameResolver();
			this.log = new DefaultApplicationErrorLogger();
			log.FileNameResolver = res;
		}

		[Test]
		public void writes_error_entry() {
			try {
				throw new Exception("message!!!");
			}catch(Exception ex) {
				log.error(ex,ctx);
			}
			var file = res.Resolve(log.FileName, true, null, ctx);
			Assert.NotNull(file);
			var result = File.ReadAllText(file);
			Assert.True(result.Contains(@"RenderType=""js"""));
		}
	}
}
